package com.hitsz.application;

import androidx.appcompat.app.AppCompatActivity;
import com.hitsz.R;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AddCatActivity extends AppCompatActivity implements View.OnClickListener {

    String nature_id = "";
    String breed_id = "";
    String color_id = "";
    String haunt_id = "";
    String cat_name = "";


    OkHttpClient okHttpClient = new OkHttpClient();
    String TAG = "Cat";
    public String res = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_cat);

        findViewById(R.id.add_cat_button).setOnClickListener(this);

        // 初始化下拉栏
        Spinner haunt_spinner = findViewById(R.id.haunt_spinner);
        Spinner color_spinner = findViewById(R.id.color_spinner);
        Spinner nature_spinner = findViewById(R.id.nature_spinner);
        Spinner breed_spinner = findViewById(R.id.breed_spinner);


        // 设置下拉栏适配器
        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.colors, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        color_spinner.setAdapter(adapter1);

        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.spots, android.R.layout.simple_spinner_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        haunt_spinner.setAdapter(adapter2);

        ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this, R.array.natures, android.R.layout.simple_spinner_item);
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        nature_spinner.setAdapter(adapter3);

        ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(this, R.array.breeds, android.R.layout.simple_spinner_item);
        adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        breed_spinner.setAdapter(adapter4);

        haunt_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                int id = i + 1;
                haunt_id = id + "";
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        color_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                int id = i + 1;
                color_id = id + "";
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        nature_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                int id = i + 1;
                nature_id = id + "";
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        breed_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                int id = i + 1;
                breed_id = id + "";
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.add_cat_button){
            // 获取猫名
            EditText cat_name_et = (EditText)findViewById(R.id.cat_name_editText);
            cat_name = cat_name_et.getText().toString().trim();

            postSend(this.cat_name, this.haunt_id, this.breed_id, this.nature_id, this.color_id);
            // 睡3秒钟等待响应
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // res处理
            if(res.equals("")){
                Toast toast=Toast.makeText(this, "响应超时！",Toast.LENGTH_SHORT);
                toast.show();
            }else if(res.contains("\"status\":0")){
                Toast toast=Toast.makeText(this, "新增猫猫成功！",Toast.LENGTH_SHORT);
                toast.show();

                // 页面跳转
                Intent intent = new Intent(AddCatActivity.this, ModeSelectActivity.class);
                startActivity(intent);
                this.finish();
            }
            else{
                Toast toast=Toast.makeText(this, "新增猫猫失败！",Toast.LENGTH_SHORT);
                toast.show();
            }

        }
    }

    // 按下返回键回到主菜单
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            Intent intent = new Intent(AddCatActivity.this, ModeSelectActivity.class);
            startActivity(intent);
            // 退出这个界面
            this.finish();
        }
        return true;
    }

    public void postSend(String cat_name, String haunt_id, String breed_id, String nature_id, String color_id){
        new Thread(){
            @Override
            public void run(){
                FormBody formBody = new FormBody.Builder()
                        .add("haunt_id", haunt_id)
                        .add("breed_id", breed_id)
                        .add("nature_id", nature_id)
                        .add("color_id", color_id)
                        .add("cat_name",cat_name).build();
                Request request = new Request.Builder()
                        .url("http://192.168.56.1:3007/api/insertCat")
                        .post(formBody).build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    res = response.body().string();
                    Log.i(TAG,"body:"+res);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}